package com.kyb.service;

import org.apache.log4j.Logger;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kyb.config.Env;
import com.kyb.dao.IDepartmentDao;
import com.kyb.dao.IUserDao;
import com.kyb.entity.Department;
import com.kyb.entity.User;
import com.kyb.impl.DepartmentDaoImpl;
import com.kyb.impl.UserDaoImpl;
import com.kyb.util.AuthHelper;
import com.kyb.util.HttpHelper;

public class DepartmentServcie {
	private static final Logger LOG = Logger.getLogger(DepartmentServcie.class);
	//1.获取部门列表
    private static final String LIST_DEPARTMENT_URL="https://oapi.dingtalk.com/department/list?access_token=ACCESS_TOKEN&id=ID";
    //2.获取部门详情
    private static final String GET_DEPARTMENTDETAIL_URL="https://oapi.dingtalk.com/department/get?access_token=ACCESS_TOKEN&id=ID";
    //3.创建部门
    private static final String CREATE_DEPARTMENT_URL="https://oapi.dingtalk.com/department/create?access_token=ACCESS_TOKEN";
    //4.更新部门
    private static final String UPDATE_DEPARTMENT_URL="https://oapi.dingtalk.com/department/update?access_token=ACCESS_TOKEN";
    //5.删除部门
    private static final String DELETE_DEPARTMENT_URL="https://oapi.dingtalk.com/department/delete?access_token=ACCESS_TOKEN&id=ID";
    //6.查询部门的所有上级父部门路径
    private static final String LIST_PARENTDEPTS_BYDEPT_URL="https://oapi.dingtalk.com/department/list_parent_depts_by_dept?access_token=ACCESS_TOKEN&id=ID";
    //7.查询指定用户的所有上级父部门路径
    private static final String LIST_PARENTDEPTS_BYUSER_URL="https://oapi.dingtalk.com/department/list_parent_depts?access_token=ACCESS_TOKEN&userId=USERID";

    
    

    /** 1.获取部门列表
     * @desc ：获取指定部门下的部门列表
     *  
     * @param accessToken
     * @param departementId 父部门id
     * @return
     * @throws Exception JSONObject
     */
    public JSONObject listDepartment(String accessToken,String departmentId) throws Exception {
        //1.准备请求url
        String url=LIST_DEPARTMENT_URL.replace("ACCESS_TOKEN", accessToken).replace("ID", departmentId);

        //2.调用接口，发送请求
        JSONObject jsonObject=HttpHelper.doGet(url);

        //3.解析请求结果
        if (null != jsonObject) {  
            //4.请求成功,则返回jsonObject
            if (0==jsonObject.getInteger("errcode")) {
                return jsonObject;
            }
            //5.错误消息处理
            if (0 != jsonObject.getInteger("errcode")) {  
                int errCode = jsonObject.getInteger("errcode");
                String errMsg = jsonObject.getString("errmsg");
                throw new Exception("error code:"+errCode+", error message:"+errMsg); 
            }  
        }   

        return null;
    }
    
    
    /**
     *部门数据全部导入数据库
     * @throws Exception
     */
    public void addListDepartement() throws Exception {
        String accessToken=AuthHelper.getAccessToken(Env.CORP_ID, Env.CORP_SECRET);
        String departmentId="1";
        DepartmentServcie departmentServcie= new DepartmentServcie();      
        JSONObject jsonObject=departmentServcie.listDepartment(accessToken, departmentId);
        //获取部门列表
        JSONArray jsonArray= (JSONArray) jsonObject.get("department");
        for(int i = 0; i < jsonArray.size() ; i ++) {
        	JSONObject obj= (JSONObject) jsonArray.get(i);
        	IDepartmentDao departmentDao = new DepartmentDaoImpl();
        	boolean flag = departmentDao.addDepartment(setDepInfo(obj));
        	
        	if(flag){
        		LOG.info(obj.get("name")+"部门,信息新增完毕!");
    		}else{
    			LOG.info(obj.get("name")+"部门信息新增失败!");
    		}
        }
    }
    
    
    /**
     * 设置部门信息
     * @param obj
     * @return
     */
    private Department setDepInfo(JSONObject obj) {
    	Department department  =new Department();
    	department.setId((int) obj.get("id"));
    	department.setName((String) obj.get("name"));
    	department.setParentId(obj.get("parentid").toString());
    	return department;
	}
    
    
    /**
     * 将钉钉同步的数据插入数据库
     */
    public void insertDeptInfo(Department department) {
    	IDepartmentDao departmentDao = new DepartmentDaoImpl(); 
    	boolean flag = departmentDao .addDepartment(department);
    	if (flag) {
    		LOG.info("新增了"+department.getName()+"部门,编号为:"+department.getId());
		} else {
			LOG.info(department.getName() + "信息新增失败!");
		}
    	
    }
    
    /**
	 * 更新人员信息到本地数据库
	 */
	public void updateDeptInfo(Department department) {
		IDepartmentDao departmentDao = new DepartmentDaoImpl(); 
		boolean flag = departmentDao.updateDepartment(department);
		if(flag) {
			LOG.info("修改了"+department.getName()+"部门,编号为:"+department.getId());
		}else {
			LOG.info(department.getName() + ",信息更新失败!");
		}
		
	}
	
	/**
	 * 删除钉钉同步的部门信息
	 * @param user
	 * @param userId
	 */
	public void deleteDeptInfo(String deptId) {
		IDepartmentDao departmentDao = new DepartmentDaoImpl(); 
		boolean flag = departmentDao.deleteDepartment(deptId);
		if(flag) {
			LOG.info("删除了一个部门,编号为:"+deptId);
		}else {
			LOG.info("部门信息删除失败!");
		}
		
	}
    
    
    

	/** 2.获取部门详情
     * @desc ：
     *  
     * @param accessToken
     * @param departementId 部门id
     * @return
     * @throws Exception JSONObject
     */
    public JSONObject getDepartmentDetail(String accessToken,String departmentId) throws Exception {
        //1.准备请求url
        String url=GET_DEPARTMENTDETAIL_URL.replace("ACCESS_TOKEN", accessToken).replace("ID", departmentId);

        //2.调用接口，发送请求
        JSONObject jsonObject=HttpHelper.doGet(url);
        System.out.println(jsonObject);

        //3.解析请求结果
        if (null != jsonObject) {  
            //4.请求成功,则返回jsonObject
            if (0==jsonObject.getInteger("errcode")) {
                return jsonObject;
            }
            //5.错误消息处理
            if (0 != jsonObject.getInteger("errcode")) {  
                int errCode = jsonObject.getInteger("errcode");
                String errMsg = jsonObject.getString("errmsg");
                throw new Exception("error code:"+errCode+", error message:"+errMsg); 
            }  
        }   

        return null;
    }
    
    
    
    


    /** 3.创建部门
     * @desc ：
     *  
     * @param accessToken
     * @param department
     * @return
     * @throws Exception String
     */
    public String createDepartment(String accessToken,Department department) throws Exception {
        //1.准备POST请求参数
        Object data=JSON.toJSON(department);
        System.out.println(data);

        //2.获取请求url
        String url=CREATE_DEPARTMENT_URL.replace("ACCESS_TOKEN", accessToken);

        //3.发起POST请求，获取返回结果
        JSONObject jsonObject=HttpHelper.doPost(url, data);
        System.out.println("jsonObject:"+jsonObject.toString());

        //4.解析结果，获取UserId
        String departmentId="";
        if (null != jsonObject) {  
            departmentId=jsonObject.getString("id");
            //5.错误消息处理
            if (0 != jsonObject.getInteger("errcode")) {  
                int errCode = jsonObject.getInteger("errcode");
                String errMsg = jsonObject.getString("errmsg");
                throw new Exception("error code:"+errCode+", error message:"+errMsg); 
            }  
        }   

        return departmentId;
    }


    /** 4.更新部门
     * @desc ：
     *  
     * @param accessToken
     * @param department    其中id是必须的
     * @return
     * @throws Exception String
     */
    public void updateDepartment(String accessToken,Department department) throws Exception {
        //1.准备POST请求参数
        Object data=JSON.toJSON(department);
        System.out.println(data);

        //2.获取请求url
        String url=UPDATE_DEPARTMENT_URL.replace("ACCESS_TOKEN", accessToken);

        //3.发起POST请求，获取返回结果
        JSONObject jsonObject=HttpHelper.doPost(url, data);
        System.out.println("jsonObject:"+jsonObject.toString());

        //4.解析结果
        if (null != jsonObject) {  
            //5.错误消息处理
            if (0 != jsonObject.getInteger("errcode")) {  
                int errCode = jsonObject.getInteger("errcode");
                String errMsg = jsonObject.getString("errmsg");
                throw new Exception("error code:"+errCode+", error message:"+errMsg); 
            }  
        }   

    }

    /**5.删除部门
     * @desc ：
     *  
     * @param accessToken
     * @param departmentId
     * @throws Exception void
     */
      public void deleteDepartment(String accessToken,String departmentId) throws Exception {
        //1.准备请求url
          String url=DELETE_DEPARTMENT_URL.replace("ACCESS_TOKEN", accessToken).replace("ID", departmentId);

          //2.调用接口，发送请求
          JSONObject jsonObject=HttpHelper.doGet(url);
          System.out.println(jsonObject);

          //3.解析请求结果
          if (null != jsonObject) {  
          
              //5.错误消息处理
              if (0 != jsonObject.getInteger("errcode")) {  
                  int errCode = jsonObject.getInteger("errcode");
                  String errMsg = jsonObject.getString("errmsg");
                  throw new Exception("error code:"+errCode+", error message:"+errMsg); 
              }  
          }   

      
      }

      /** 6.查询部门的所有上级父部门路径
       * @desc ：
       *  返回的结果按顺序依次为其所有父部门的ID，直到根部门
       * @param accessToken
       * @param departementId 部门id
       * @return
       * @throws Exception JSONObject
       */
      public JSONObject listParentDepartementsByDept(String accessToken,String departmentId) throws Exception {
          //1.准备请求url
          String url=LIST_PARENTDEPTS_BYDEPT_URL.replace("ACCESS_TOKEN", accessToken).replace("ID", departmentId);

          //2.调用接口，发送请求
          JSONObject jsonObject=HttpHelper.doGet(url);
          System.out.println(jsonObject);

          //3.解析请求结果
          if (null != jsonObject) {  
              //4.请求成功,则返回jsonObject
              if (0==jsonObject.getInteger("errcode")) {
                  return jsonObject;
              }
              //5.错误消息处理
              if (0 != jsonObject.getInteger("errcode")) {  
                  int errCode = jsonObject.getInteger("errcode");
                  String errMsg = jsonObject.getString("errmsg");
                  throw new Exception("error code:"+errCode+", error message:"+errMsg); 
              }  
          }   

          return null;
      }
      
      
      /** 7.查询指定用户的所有上级父部门路径
       * @desc ：
       *  返回的结果按顺序依次为其所有父部门的ID，直到根部门
       * @param accessToken
       * @param departementId 部门id
       * @return
       * @throws Exception JSONObject
       */
      public JSONObject listParentDepartementsByUser(String accessToken,String userID) throws Exception {
          //1.准备请求url
          String url=LIST_PARENTDEPTS_BYUSER_URL.replace("ACCESS_TOKEN", accessToken).replace("USERID", userID);
          
          //2.调用接口，发送请求
          JSONObject jsonObject=HttpHelper.doGet(url);
          System.out.println(jsonObject);
          
          //3.解析请求结果
          if (null != jsonObject) {  
              //4.请求成功,则返回jsonObject
              if (0==jsonObject.getInteger("errcode")) {
                  return jsonObject;
              }
              //5.错误消息处理
              if (0 != jsonObject.getInteger("errcode")) {  
                  int errCode = jsonObject.getInteger("errcode");
                  String errMsg = jsonObject.getString("errmsg");
                  throw new Exception("error code:"+errCode+", error message:"+errMsg); 
              }  
          }   
          
          return null;
      }
      
   

}
